package edu.gduf.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.gduf.dao.SigninMapper;
import edu.gduf.dao.StudentMapper;
import edu.gduf.dto.SigninSystemResult;
import edu.gduf.entity.Student;
import edu.gduf.service.StudentService;

/**
 * Created by hrm on 2017/4/8.
 */
@Service
public class StudentServiceImpl implements StudentService{
	@Autowired
	private StudentMapper studentMapper;

	@Autowired
	private SigninMapper signinMaper;

    @Override
    public SigninSystemResult executeLogin(int stuId, String password) {
		Student student = studentMapper.selectByIdAndPassword(stuId, password);
		SigninSystemResult<Student> signinSystemResult = new SigninSystemResult<Student>();
		if (student == null) {
			signinSystemResult.setSuccess(false);
			signinSystemResult.setMsg("用户名或密码错误");
		} else {
			signinSystemResult.setSuccess(true);
			signinSystemResult.setMsg("登录成功");
			signinSystemResult.setData(student);
		}
		return signinSystemResult;
    }

    @Override
    public SigninSystemResult updatePassword(int stuId, String sPassword, String dPassword) {
		Student stuid2 = studentMapper.selectByIdAndPassword(stuId, sPassword);
		SigninSystemResult signinSystemResult = new SigninSystemResult();
		if (stuid2 == null) {
			signinSystemResult.setMsg("旧密码错误");
			signinSystemResult.setSuccess(false);
		} else {
			Student student = new Student();
			student.setStuId(stuId);
			student.setStuPassword(dPassword);
			studentMapper.updateByPrimaryKeySelective(student);
			signinSystemResult.setMsg("修改密码成功");
			signinSystemResult.setSuccess(true);
		}

		return signinSystemResult;
    }

    @Override
    public SigninSystemResult updateEmail(int tId, String tEmail) {
		SigninSystemResult signinSystemResult = new SigninSystemResult();
		try {
			Student student = new Student();
			student.setStuId(tId);
			student.setStuEmail(tEmail);
			studentMapper.updateByPrimaryKeySelective(student);
			signinSystemResult.setSuccess(true);
			signinSystemResult.setMsg("修改邮箱成功");
		} catch (Exception e) {
			signinSystemResult.setSuccess(false);
			signinSystemResult.setMsg("修改邮箱失败");
		}
		return signinSystemResult;
    }

	@Override
	public List<Student> selectByClassId(int classId) {
		return studentMapper.selectByClassId(classId);
	}

	

}
